using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using SypEngine;
using SypEngine.SiteConfig;
using SypConst.Garbage;
using SypConst.Path;

namespace SypEngine
{
    public class SypAlbumVisitor : SypVisitor
    {
        public SypAlbumVisitor(SetText WriteMethod, VisitorCallBack[] Callbacks)
            : base(WriteMethod, Callbacks)
        {
        }

        public void VisitShowbrowserfileMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 1)
            {
                int SiteId = -1;
                if (int.TryParse(parameters[1], out SiteId))
                {
                    if (Callbacks.Length  > 1)
                        Callbacks[1](SiteId);
                }
                else
                    OutputMethod("Showbrowserfile-album : cannot convert " + SiteId + "in int type.", Output.ERROR);
            }
            else
                OutputMethod("Showbrowserfile-album : missing parameters", Output.ERROR);
        }

        public void VisitShowbrowserMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 1)
            {
                int SiteId = -1;
                if (int.TryParse(parameters[1], out SiteId))
                {
                    if (Callbacks.Length > 0)
                        Callbacks[0](SiteId);
                }
                else
                    OutputMethod("Showbrowser-album : cannot convert " + SiteId + "in int type.", Output.ERROR);
            }
            else
                OutputMethod("Showbrowser-album : missing parameters", Output.ERROR);
        }

        public void VisitAddfromfileMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 4)
            {
                int SiteId = -1;
                int CategoryId = -1;
                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                    CategoryId = int.Parse(cleanArg(parameters[2]));
                }
                catch (Exception)
                {
                    OutputMethod("AddFromFile-album : one or more parameters are not a number", Output.ERROR);
                    return;
                }
                SypSite mySite = SiteObj.GetSiteConfig(SiteId);

                string FolderToImport = cleanArg(parameters[3]);
                string FolderName = cleanArg(parameters[4]);
                if (mySite != null)
                {
                    OutputMethod(SiteId.ToString() + SypGarbage.COMMAND_SEPARATOR + "0" + SypGarbage.COMMAND_SEPARATOR + "100", Output.LOCAL_STEP);
                    if (Directory.Exists(FolderToImport))
                    {
                        if (mySite.CategorysList != null)
                        {
                            foreach (Category cat in mySite.CategorysList)
                            {
                                if (cat.AlbumList == null)
                                    cat.AlbumList = new List<Album>();
                                else
                                {
                                    foreach (Album alb in cat.AlbumList)
                                    {
                                        SypPage EditAlbumPage = new SypPage(alb.id, SiteId, OutputMethod, mySite.Language);
                                        SiteObj.AddAction(mySite.SiteId, EditAlbumPage, SyncAction.ADD_FILE, SyncMode.LOCAL);

                                        string onlineAlbumPage = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, mySite.SiteId, cat.Id, alb.id);
                                        SiteObj.AddAction(mySite.SiteId, onlineAlbumPage, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                        //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + sect.id + "/" + SypGarbage.ALBUM_PAGE_PREF + sect.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                    }
                                }
                                if (cat.Id == CategoryId)
                                {
                                    //Add album folder
                                    DirectoryInfo FolderInfo = new DirectoryInfo(FolderToImport);
                                    Album newAlbum = null;
                                    if (FolderName != "")
                                        newAlbum = new Album(FolderName,  FolderToImport);
                                    else
                                        newAlbum = new Album(FolderInfo.Name, FolderToImport);

                                    mySite.AlbumIdToReload = newAlbum.id;
                                    //string albumLocalDir = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.ALBUM_FOLDER, null, SiteId, alb.Id, newAlbum.id);
                                    //string albumOnlineDir = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_FOLDER, null, SiteId, alb.Id, newAlbum.id);

                                    //SiteObj.AddAction(mySite.SiteId, albumLocalDir, SyncAction.ADD_FOLDER, SyncMode.LOCAL);
                                    //SiteObj.AddAction(mySite.SiteId, albumOnlineDir, SyncAction.ADD_FOLDER, SyncMode.ONLINE);

                                    //Add album page
                                    SypPage myPage = new SypPage(newAlbum.id, SiteId, OutputMethod, mySite.Language);
                                    SiteObj.AddAction(mySite.SiteId, myPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                                    //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + CategoryId + "/" + SypGarbage.ALBUM_PREF_DIR + newAlbum.id + "/" + SypGarbage.ALBUM_PAGE_PREF + newAlbum.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                    SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, SiteId, cat.Id, newAlbum.id), SyncAction.ADD_FILE, SyncMode.ONLINE);

                                    cat.AlbumList.Add(newAlbum);
                                   

                                    //Add picture page
                                    string[] files = SypGarbage.GetFiles(FolderToImport, SypGarbage.PICTURE_FILTER);
                                    foreach (string file in files)
                                    {
                                       // if (SypGarbage.CheckPictureFile(file))
                                       // {
                                            FileInfo PictureInfo = new FileInfo(file);
                                            Picture newPicture = new Picture(PictureInfo.Name, file);
                                            SypGarbage.free(PictureInfo);

                                            //Copy picture
                                            SiteObj.AddAction(mySite.SiteId, new string[] {file, newPicture.Id.ToString() } , SyncAction.COPY_PICTURE, SyncMode.LOCAL);
                                            //SiteObj.AddAction(mySite.SiteId, SypGarbage.PICTURES_FOLDER + SypGarbage.PICTURE_PREF + newPicture.Id + SypGarbage.PICTURE_EXTENSION, SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                            //SiteObj.AddAction(mySite.SiteId, SypGarbage.THUMBNAILS_FOLDER + SypGarbage.THUMBNAIL_PREF + newPicture.Id + SypGarbage.THUMBNAIL_EXTENSION, SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                            SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.PICTURE_FILE, null, SiteId, -1, -1, newPicture.Id), SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                            SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.THUMBNAIL_FILE, null, SiteId, -1, -1, newPicture.Id), SyncAction.COPY_PICTURE, SyncMode.ONLINE);


                                            if (newAlbum.PicturesList == null)
                                                newAlbum.PicturesList = new List<Picture>();
                                            newAlbum.PicturesList.Add(newPicture);
                                       // }
                                    }
                                }
                            }
                        }
                    }
                    else
                        OutputMethod("[" + SiteId + "]AddFromFile-album : directory not found, "+FolderToImport, Output.ERROR);
                }
                else
                    OutputMethod("[" + SiteId + "]AddFromFile-album : siteid " + SiteId + " not found", Output.ERROR);
            }
            else
                OutputMethod("AddFromFile-album : missing parameters", Output.ERROR);
        }

        public override void VisitAddMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 4)
            {
                int SiteId = -1;
                int CategoryId = -1;
                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                    CategoryId = int.Parse(cleanArg(parameters[2]));
                }
                catch (Exception)
                {
                    OutputMethod("Add-album : one or more parameters are not a number", Output.ERROR);
                    return;
                }
                SypSite mySite = SiteObj.GetSiteConfig(SiteId);

                string AlbumTitle = cleanArg(parameters[3]);
                if (mySite != null)
                {
                    if (mySite.CategorysList != null)
                    {
                        foreach (Category cat in mySite.CategorysList)
                        {
                            if (cat.AlbumList == null)
                                cat.AlbumList = new List<Album>();
                            else
                            {
                                foreach (Album alb in cat.AlbumList)
                                {
                                    SypPage EditAlbumPage = new SypPage(alb.id, SiteId,OutputMethod, mySite.Language);
                                    SiteObj.AddAction(mySite.SiteId, EditAlbumPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                                    //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PAGE_PREF + sect.id + "/" + SypGarbage.ALBUM_PAGE_PREF + sect.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                    SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, -1, cat.Id, alb.id, -1), SyncAction.ADD_FILE, SyncMode.ONLINE);
                                }
                            }
                            if (cat.Id == CategoryId)
                            {
                                //Add album folder
                                Album newAlbum = new Album(AlbumTitle, null);
                                //string albumLocalDir = mySite.LocalDirectory + SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + newAlbum.id;
                                //string albumOnlineDir = SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + newAlbum.id;
                                //string albumLocalDir = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.ALBUM_FOLDER, null, SiteId, alb.Id, newAlbum.id);
                                //string albumOnlineDir = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_FOLDER, null, -1, alb.Id, newAlbum.id);
                                //SiteObj.AddAction(mySite.SiteId, albumLocalDir, SyncAction.ADD_FOLDER, SyncMode.LOCAL);
                               // SiteObj.AddAction(mySite.SiteId, albumOnlineDir, SyncAction.ADD_FOLDER, SyncMode.ONLINE);

                                //Add album page
                                SypPage myPage = new SypPage(newAlbum.id, SiteId, OutputMethod, mySite.Language);
                                SiteObj.AddAction(mySite.SiteId, myPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                                //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + CategoryId + "/" + SypGarbage.ALBUM_PREF_DIR + newAlbum.id + "/" + SypGarbage.ALBUM_PAGE_PREF + newAlbum.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, -1, CategoryId, newAlbum.id), SyncAction.ADD_FILE, SyncMode.ONLINE);


                                cat.AlbumList.Add(newAlbum);
                                mySite.AlbumIdToReload = newAlbum.id;
                            }
                        }
                    }
                }
                else
                    OutputMethod("[" + SiteId + "]Add-album : siteid " + SiteId + " not found", Output.ERROR);
            }
            else
                OutputMethod("Add-album : missing parameters", Output.ERROR);

        }

        public override void VisitEditMethod(SypSiteHandler SiteObj,  string[] parameters)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void VisitSupprMethod(SypSiteHandler SiteObj,  string[] parameters)
        {
            if (parameters.Length > 2)
            {
                int SiteId = -1;
                int AlbumId = -1;
                int AlbumVisitedId = -1;
                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                    AlbumId = int.Parse(cleanArg(parameters[2]));

                    if (parameters.Length > 3)
                        AlbumVisitedId = int.Parse(cleanArg(parameters[3]));
                }
                catch (Exception)
                {
                    OutputMethod("Suppr-album : one or more parameters are not a number", Output.ERROR);
                    return;
                }
                SypSite mySite = SiteObj.GetSiteConfig(SiteId);
                OutputMethod(SiteId.ToString() + SypGarbage.COMMAND_SEPARATOR + "0" + SypGarbage.COMMAND_SEPARATOR + "100", Output.LOCAL_STEP);
                if (mySite != null)
                {
                    Album albToSuppr = null;
                    if (mySite.CategorysList != null)
                    {
                        foreach (Category cat in mySite.CategorysList)
                        {
                            if (cat.AlbumList != null)
                            {
                                foreach (Album alb in cat.AlbumList)
                                {
                                    if (alb.id == AlbumId)
                                    {
                                        //Suppr album folder
                                        albToSuppr = alb;
                                        //string albumLocalDir = mySite.LocalDirectory + SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + AlbumId;
                                        //string albumOnlineDir = SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + AlbumId;
                                        //string albumLocalDir = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.ALBUM_FOLDER, null, SiteId, alb.Id, sect.id, -1);
                                        //string albumOnlineDir = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_FOLDER, null, -1, alb.Id, sect.id, -1);
                                        //SiteObj.AddAction(mySite.SiteId, albumLocalDir, SyncAction.SUPPR_FOLDER, SyncMode.LOCAL);
                                        //SiteObj.AddAction(mySite.SiteId, albumOnlineDir, SyncAction.SUPPR_FOLDER, SyncMode.ONLINE);

                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.ALBUM_PAGE, null, mySite.SiteId, cat.Id, AlbumId), SyncAction.SUPPR_FILE, SyncMode.LOCAL);
                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, -1, cat.Id, AlbumId), SyncAction.SUPPR_FILE, SyncMode.ONLINE);

                                        //Suppr pictures and thumbnail
                                        foreach (Picture pict in alb.PicturesList)
                                        {
                                            //string thumbnailfile = mySite.LocalDirectory + SypGarbage.THUMBNAILS_FOLDER + SypGarbage.THUMBNAIL_PREF + pict.Id + SypGarbage.THUMBNAIL_EXTENSION;
                                            //string picturefile = mySite.LocalDirectory + SypGarbage.PICTURES_FOLDER + SypGarbage.PICTURE_PREF + pict.Id + SypGarbage.PICTURE_EXTENSION;
                                            string thumbnailfile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.THUMBNAIL_FILE, null, SiteId, -1, -1, pict.Id);
                                            string picturefile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.PICTURE_FILE, null, SiteId, -1, -1, pict.Id);
                                            SiteObj.AddAction(mySite.SiteId, thumbnailfile, SyncAction.SUPPR_FILE, SyncMode.LOCAL);
                                            SiteObj.AddAction(mySite.SiteId, picturefile, SyncAction.SUPPR_FILE, SyncMode.LOCAL);

                                            //thumbnailfile = SypGarbage.THUMBNAILS_FOLDER + SypGarbage.THUMBNAIL_PREF + pict.Id + SypGarbage.THUMBNAIL_EXTENSION;
                                            //picturefile = SypGarbage.PICTURES_FOLDER + SypGarbage.PICTURE_PREF + pict.Id + SypGarbage.PICTURE_EXTENSION;
                                            thumbnailfile = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.THUMBNAIL_FILE, null, -1, -1, -1, pict.Id);
                                            picturefile = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.PICTURE_FILE, null, -1, -1, -1, pict.Id);
                                            SiteObj.AddAction(mySite.SiteId, thumbnailfile, SyncAction.SUPPR_FILE, SyncMode.ONLINE);
                                            SiteObj.AddAction(mySite.SiteId, picturefile, SyncAction.SUPPR_FILE, SyncMode.ONLINE);

                                        }
                                        //SiteObj.AddAction(mySite.SiteId, albumOnlineDir + "/" + SypGarbage.ALBUM_PAGE_PREF + sect.id + SypGarbage.PAGE_EXTENSION, SyncAction.SUPPR_FILE, SyncMode.ONLINE);
                                        //SiteObj.AddAction(mySite.SiteId, albumOnlineDir, SyncAction.SUPPR_FOLDER, SyncMode.ONLINE);
                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, -1, cat.Id, alb.id, -1), SyncAction.SUPPR_FILE, SyncMode.ONLINE);
                                       // SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_FOLDER, null, -1, alb.Id, sect.id, -1), SyncAction.SUPPR_FOLDER, SyncMode.ONLINE);
                                    }
                                    else
                                    {
                                        SypPage EditAlbumPage = new SypPage(alb.id, SiteId, OutputMethod, mySite.Language);
                                        SiteObj.AddAction(mySite.SiteId, EditAlbumPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                                        //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + sect.id + "/" + SypGarbage.ALBUM_PAGE_PREF + sect.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, -1, cat.Id, alb.id, -1), SyncAction.ADD_FILE, SyncMode.ONLINE);
                                    }

                                    
                                }
                                if (albToSuppr != null)
                                {
                                    cat.AlbumList.Remove(albToSuppr);
                                    if (AlbumVisitedId != AlbumId)
                                        mySite.AlbumIdToReload = AlbumVisitedId;
                                }
                            }
                        }
                    }
                }
                else
                    OutputMethod("[" + SiteId + "]Add-album : siteid " + SiteId + " not found", Output.ERROR);
            }
            else
                OutputMethod("Add-album : missing parameters", Output.ERROR);
        }

        
    }
}
